// 替换组件
import('@/tysrc/component-change-rule/ts-web.js');
// 替换/扩展 多语言
import('@/tysrc/lang-change-rule/ts-web.js');
// 替换路由
import { registerRouters } from '@/tysrc/router-change-rule/ts-web.js';

// 注册路由
const { app, core, router, tyGetExportResource, tyGetMixinResource } = window.tyRoot;
registerRouters(app, router);

// 非二开环境，挂载core 和 dom
if (process.env.VUE_APP_CURRENTMODE !== 'sdk') {
  // 注入core
  app.use(core);
  // 挂载到app中
  app.mount('#plm3-app');
  // modify by caoy 2024/04/08 取消异步加载路由的方式 所以微前端路由跳转须在core加载后进行
  // 微前端模式下跳转到设置的指定路由
  if (window.__POWERED_BY_WUJIE__ && window.$wujie?.props.path) {
    router.push(window.$wujie?.props.path);
  }
} else {
  const tyGetExportResource2 = url => {
    const context = require.context('./', true, /\.(js|vue)$/);
    return context('./' + url.replace(/^(src\/|@\/)/, ''));
  };
  // 提供二开的hooks
  window.tyRoot = Object.assign({}, window.tyRoot || {}, {
    app,
    core,
    router,
    // commonjs的包导出方式获取资源直接使用export导出资源
    tyGetExportResource: url => {
      const rts = tyGetExportResource(url);
      if (rts) {
        return rts;
      }
      return tyGetExportResource2(url);
    },
    // commonjs的包导出方式获取资源对下也可以获取组件（静态资源或者mixin）
    tyGetMixinResource: url => {
      const rts = tyGetMixinResource(url);
      if (rts) {
        return rts?.default;
      }
      return tyGetExportResource2(url)?.default;
    },
    // 异步获取资源
    tyGetViewResource: url => {
      return () => {
        const rts = tyGetExportResource(url);
        if (rts) {
          return Promise.resolve(rts);
        }
        return Promise.resolve(tyGetExportResource2(url));
      };
    }
  });
}
